package databases

import (
	"fmt"
	"gin_web/config"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"net/url"
)

var DB *gorm.DB

func InitDB() {
	datasource := config.GConfig.Datasource

	args := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=%s&parseTime=true&loc=%s",
		datasource.Username,
		datasource.Password,
		datasource.Host,
		datasource.Port,
		datasource.Database,
		datasource.Charset,
		url.QueryEscape(datasource.Loc))

	db, err := gorm.Open(mysql.Open(args), &gorm.Config{})
	if err != nil {
		panic("failed to connect database")
	}
	DB = db
}
